# -*- coding: utf-8 -*-

import numpy as np;
import matplotlib.pyplot as mp;

from eod import *

## Constantes des equations
eps = 0.1;

N0 = 200.; # Population initiales
b=0.4; # births
d=0.3; # deaths
gamma = b-d;
kappa = 300; # Coef de verhulst ...
PP = np.array([2,2]); # Proies / Predateur
la = 0.5;
lb = 1; # b = c , normalisation du système
lc = 1;
ld = 1.5;


## Modèles d'equations
malthus = (1, 0, N0, lambda y, t: b*y - d*y); # Modèle de Malthus
verhulst = (1, 0, N0, lambda y, t: gamma * y *(1 - y/kappa)); # Modèle de Verhulst
lotka = (2, 0., PP, lambda y, t: np.array([ y[0]*(la-lb*y[1]) , y[1]*(lc*y[0]-ld) ])); # Modèle proie/prédateur

## Graphes



####################################
# methode opti

# Malthus
mp.clf();
X = np.arange(0., 40., 0.5)
print "Calcul de malthus ...";
Ymal = fill_graph(malthus, X, eps, step_euler);
fmal = mp.plot(X, Ymal, color='red', label='pouet');
mp.title("Malthus - N0 = "+str(N0)+", gamma = "+str(gamma));
mp.savefig("malthus.png");

# Verhulst
mp.clf();
X = np.arange(0., 80., 0.1);
print "Calcul de verhulst ...";
Yver = fill_graph(verhulst, X, eps, step_euler);
fver = mp.plot(X, Yver, color='blue');
mp.title("Verhulst - N0 = "+str(N0)+", K = "+str(kappa));
mp.savefig("verhulst.png");

# Lotka-Volterra normal
mp.clf();
X = np.arange(0., 80., 0.1);
eps = 0.01
print "Calcul de lotka ...";
Ylot = fill_graph(lotka, X, eps, step_euler);
fproie = mp.plot(X, Ylot[:,0], color='red');
fpred = mp.plot(X, Ylot[:,1], color='blue');
mp.legend([fproie[0], fpred[0]], ["Proies", "Predateurs"]); 
mp.title("Lotka N0 = "+str(PP[0])+" P0 = "+str(PP[1])+" a = "+str(la)+" d = "+str(ld));
mp.savefig("lotka.png");

periode = compute_period(X,Ylot[:,0]);
print "Periode de lotka : " +str(periode);

# Lotka-Volterra constant
PP = np.array([ld, la]); # Proies / Predateur
lotka = (2, 0., PP, lambda y, t: np.array([ y[0]*(la-lb*y[1]) , y[1]*(lc*y[0]-ld) ]));
mp.clf();
X = np.arange(0., 80., 0.1);
eps = 0.01
print "Calcul de lotka constant ...";
Ylot = fill_graph(lotka, X, eps, step_euler);
fproie = mp.plot(X, Ylot[:,0], color='red');
fpred = mp.plot(X, Ylot[:,1], color='blue');
mp.legend([fproie[0], fpred[0]], ["Proies", "Predateurs"]); 
mp.title("Lotka N0 = "+str(PP[0])+" P0 = "+str(PP[1])+" a = "+str(la)+" d = "+str(ld));
mp.savefig("lotkacons.png");




#Phases de lotka
mp.clf();
PP = np.array([2,2]); # Proies / Predateur
X = np.arange(0., 10, 0.1);
lotka = (2, 0., PP, lambda y, t: np.array([ y[0]*(la-lb*y[1]) , y[1]*(lc*y[0]-ld) ]));
print "Calcul de phases lotka 1 ...";
Ylot = fill_graph(lotka, X, eps, step_euler);
mp.plot(Ylot[:,0], Ylot[:,1], color='red');
PP = np.array([1,1]); # Proies / Predateur
lotka = (2, 0., PP, lambda y, t: np.array([ y[0]*(la-lb*y[1]) , y[1]*(lc*y[0]-ld) ]));
print "Calcul de phases lotka 2 ...";
Ylot = fill_graph(lotka, X, eps, step_euler);
mp.plot(Ylot[:,0], Ylot[:,1], color='blue');
mp.savefig("lotkaphases.png");


# Variations autour de y0
PP = np.array([2,2]); # Proies / Predateur
lotka = (2, 0., PP, lambda y, t: np.array([ y[0]*(la-lb*y[1]) , y[1]*(lc*y[0]-ld) ]));
trace_local(lotka , 0.5);
mp.savefig("localvar.png");

# Variations autour de y0 =  point singulier
PP = np.array([ld,la]); # Proies / Predateur
lotka = (2, 0., PP, lambda y, t: np.array([ y[0]*(la-lb*y[1]) , y[1]*(lc*y[0]-ld) ]));
trace_local(lotka, 0.1);
mp.savefig("localvarsing.png");


print "Fin des calculs. Images créées"
exit();
####################################
# methode non opti


ymal = np.zeros(len(X));
yver = np.zeros(len(X));
ylot = np.zeros([len(X),lotka[0]]);
i=0;
for x in X:
	# Ajouter pointeur vers ensemble d'elements de fonction pour stocker donner et optimiser calculs
	ymal[i]=meth_epsilon(malthus[2], malthus[1], x,  eps, malthus[3], step_RK4);
	yver[i]=meth_epsilon(verhulst[2], verhulst[1], x,  eps, verhulst[3], step_RK4);
	ylot[i]=meth_epsilon(lotka[2], lotka[1], x,  eps, lotka[3], step_RK4);
	i+=1;

mp.clf();
fmal = mp.plot(X, ymal, color='red', label='pouet');
mp.savefig("Malthus.png");
mp.clf();
fver = mp.plot(X, yver, color='blue');
mp.savefig("verhulst.png");
mp.clf();
fproie = mp.plot(X, ylot[:,0], color='red');
fpred = mp.plot(X, ylot[:,1], color='blue');
mp.legend([fproie[0], fpred[0]], ["Proies", "Predateurs"]); 
mp.savefig("lotka.png");
mp.show();
